﻿using EkStudio.com.bean.xml.RootInterface.FieldEvent;
using System;
using System.Reflection;

namespace EkStudio.com.ui.unitbill.property
{
    public class BillUiEventToProperty
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiEventToProperty));

        private BillDataFillItem Item = null;

        private BillUiEventToProperty[] Array = null;

        private bool Simple = true;

        public BillUiEventToProperty(BillDataFillItem EventItem){
            this.Item = EventItem;
        }

        public BillUiEventToProperty(BillUiEventToProperty[] array)
        {
            this.Array = array;
            Simple = false;
        }

        public string Key
        {
            get
            {
                return Simple ? Item.Key : GetStringValue("Key");
            }

            set
            {
                if (Simple) Item.Key = value; else SetValue("Key", value);
            }
        }

        public string Caption
        {
            get
            {
                return Simple ? Item.Caption : GetStringValue("Caption");
            }

            set
            {
                if (Simple) Item.Caption = value; else SetValue("Caption", value);
            }
        }

        public string FillType
        {
            get
            {
                return Simple ? Item.FillType : GetStringValue("FillType");
            }

            set
            {
                if (Simple) Item.FillType = value; else SetValue("FillType", value);
            }
        }

        public string BindEvent
        {
            get
            {
                return Simple ? Item.Event : GetStringValue("BindEvent"); 
            }

            set
            {
                if (Simple) Item.Event = value;SetValue("BindEvent", value);
            }
        }


        public bool EventType
        {
            get
            {
                return Simple?Item.EventType:GetBoolValue("EventType");
            }

            set
            {
                if (Simple) Item.EventType = value; else SetValue("EventType", value);
            }
        }

        public string TargetTable
        {
            get
            {
                return Simple ? Item.TargetTable:GetStringValue("TargetTable") ;
            }

            set
            {
                if (Simple) Item.TargetTable = value; else SetValue("TargetTable", value);
            }
        }

        public bool CoverWrite
        {
            get
            {
                return Simple ? Item.CoverWrite : GetBoolValue("CoverWrite");
            }

            set
            {
                if (Simple) Item.CoverWrite = value; else SetValue("CoverWrite", value);
            }
        }


        public BillDataFillsqlSource DataSource
        {
            get { return Simple ? Item.SqlSource : null; }
        }

        public BillDataFillItem DataFill
        {
            get { return Item; }
        }





        private object SetValue(string key, object value)
        {

            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiEventToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiEventToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null)
                    {
                        Property.SetValue(data, value);
                        logger.Info("修改成功");
                    }
                }
            }
            return value;
        }

        private bool GetBoolValue(string key)
        {
            bool result = true;
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiEventToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiEventToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(bool))
                    {
                        object val = Property.GetValue(data);
                        if (val.Equals(false))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        private int GetIntValue(string key)
        {
            int result = 0;
            if (this.Array != null && this.Array.Length > 0)
            {
                BillUiEventToProperty data = Array[0];
                Type ClazzType = typeof(BillUiEventToProperty);
                PropertyInfo Property = ClazzType.GetProperty(key);
                if (Property != null && Property.GetMethod.ReturnType == typeof(int))
                {
                    object val = Property.GetValue(data);
                    if (val is int)
                    {
                        result = (int)val;
                    }
                }
            }
            return result;
        }

        private string GetStringValue(string key)
        {
            string result = "";
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiEventToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiEventToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(string))
                    {
                        object val = Property.GetValue(data);
                        if (val is string)
                        {
                            if (result == "") result = val.ToString();
                            else if (result != (string)val)
                            {
                                result = "";
                                break;
                            }

                        }
                    }
                }
            }
            return result;
        }
    }
}
